package zjc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import zjc.dao.UserDao;
import zjc.domain.User;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserDao userDao;

    @GetMapping("/user")
    public String list(Model model) {
        List<User> users = userDao.findAll();
        model.addAttribute("users",users);
        return "user/listUser";
    }

    @GetMapping("/user/add")
    public String add(Model model) {
        model.addAttribute("user",new User());
        return "user/addUser";
    }

    @PostMapping("/user/save")
    public String save(User user) {
        userDao.save(user);
        return "user/SuccessAdd";
    }

    @PostMapping("/user/update")
    public String update(User user) {
        userDao.update(user);
        return "user/SuccessUpdate";
    }

    @GetMapping("/user/delete/{id}")
    public String delete(@PathVariable Long id) {
        userDao.delete(id);
        return "user/SuccesDelete";
    }

    @GetMapping("/user/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        User user = userDao.findById(id);
        model.addAttribute("user",user);
        System.out.println(model);
        return "user/UpdateUser";
    }
}
